﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="../js/jquery.js" type="text/javascript"></script>
</head>
<body>
    <div id="a">
    </div>
    <input type="hidden" id="ids" name="ids" />
    <select id="selProv">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select>
    <script type="text/javascript">
        function string2Object(obj) {
            var output = "";
            if (obj) {
                output += "{";
                output += obj.toString();
                for (var mem in obj) {
                    var val = obj[mem];
                    switch (typeof (val)) {
                        case "object":
                            break;
                        default:
                            output += "--";
                            break;
                    }
                    alert(mem);
                }
                output += "}";
            }

            return output;
        }

        jQuery.fn.__createChildDropDownList = function (valContainerID, fnGetData, changeHandlerName) {
            var ddlID = "ddl_p" + $(this).attr("id");
            $(this).change(function () {
                var data = fnGetData($(this).val());
                var ddl;

                // 判断元素是否存在
                if ($("#" + ddlID).length > 0) {
                    ddl = $("#" + ddlID);
                    ddl.html("");
                }
                else {
                    var html = "<select id=\"" + ddlID + "\" onchange=\"";
                    html += "$('#" + valContainerID + "').val(this.value);";

                    if (changeHandlerName != null && changeHandlerName != "") {
                        html += " " + changeHandlerName + "(this.value);";
                    }
                    html += "\" />";

                    ddl = $(html);
                    $(this).parent().append($(ddl));
                }

                var items = data.split(",");
                for (var i = 0; i < items.length; i++) {
                    if (items[i] != "") {
                        var item = items[i].split("|");
                        var val = item[0];
                        var text = item[0];
                        if (item.length > 1) {
                            var text = item[1];
                        }

                        var opt = $("<option value=\"" + val + "\">" + text + "</option>");
                        $(ddl).append(opt);
                    }
                }

                return ddl;
            });
        };

        function getStr(val) {
            var str = "";
            for (var s = 0; s < 5; s++) {
                for (var ss = 0; ss < s; ss++) {
                    str += val.toString();
                }
                str += ",";
            }
            alert(str);
            return str;
        }
        $("#selProv").__createChildDropDownList("ids", getStr, "getStr");

    </script>
</body>
</html>
